package com.example.springwebmvcdemo.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserLoginController {
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Boolean login(String userName, String password, HttpServletRequest request) {
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        //都不为空,接下来验证账号密码是否正确
        if ("High-Earphone".equals(userName) && "111111".equals(password)) {
            //都正确,设置Session信息,保证后续逻辑
            HttpSession session = request.getSession();
            session.setAttribute("key",userName);
            return true;
        }
        return false;
    }

    @RequestMapping("/getLoginUser")
    public String getLoginUser(HttpSession session) {
        if (session.getAttribute("key") != null) {
            return (String) session.getAttribute("key");
        }
        return "";
    }
}
